; (function () {
  new Vue({
    el: '#report_bi',
    data: {
      costitemList: [],
      costitemList0: [],
      billlist: [],
      resourceList: [],
      incomeList: [{ expenses: [] }],
      companyList: [],
      ProjectList: [],
      buildingList: [],
      floorList: [],
      unitList: [],

      keyword: '',
      payment_begin_time: '',
      payment_end_time: '',
      expense_name: [],
      bill_type: [],
      contract_type: [],
      page_size: 10,
      page: 1,
      project_ids: [],
      expenseList: [],

      areaId: '',
      companyId: [],
      projectId: [],
      buildingId: [],
      floorId: [],
      unitId: [],

      limit: ['10'],
      curPage: 1,
      totalCount: 0,
      pageCounts: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      isExtenseFlag:false,
      quanxian: JSON.parse(window.localStorage.getItem('permissions')),
    },
    filters: {
      billTypeText: function (name) {
        let result;
        switch (name) {
          case 'C':
            result = '租赁账单'
            break;
          case 'T':
            result = '退租账单'
            break;
          case 'YX':
            result = '意向书账 '
            break;
          case 'YXTK':
            result = '意向书退款账单 '
            break;
          default:
            result = '--'
        }
        return result
      },
      formatDate: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
    },
    methods: {
      openSearch:function(){
        if(this.isExtenseFlag == true){
          this.isExtenseFlag = false;
          return false;
        }
        if(this.isExtenseFlag == false){
          this.isExtenseFlag = true;
          return false;
        }
      },
      // 获取所有大区
      getConfigAreas() {
        this.getcompany();
      },
      //默认所以项目
      getProjectName() {
        hlhttp.get({
          url: 'user/projects',
          success: (data) => {
            data.forEach(item => {
              this.project_ids.push(item.projectId)
            });
            this.getIncome(1, this.limit[0])
          }
        })
      },
      // 获取费项（排序）
      getConfigCostItem() {
        hlhttp.get({
          url: 'report/other_expenditures',
          success: (data) => {
						data.map(item => {
							item.code = item.name
						})
            this.costitemList = data
            // var a = { id: "0", lateFee: "0", name: "临时费项", type: "O", code: '临时费项' }
            // this.costitemList.push(a)
            this.costitemList = this.costitemList.sort(compareAsc('name'))
          }
        })
      },
      // 获取账单
      getConfigBillItem() {
        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'REPORT', config_name: 'BI_INCOME_BILL_TYPE' },
          success: (data) => {
            this.billlist = hlArrayUtil.replaceData(data, [{key: 'dictName', rKey: 'name'}, {key: 'dictCode', rKey: 'code'}]);
          }
        })
      },
      // 获取资源
      getConfigResourcesItem() {
        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'REPORT', config_name: 'BI_INCOME_RESOURCE_TYPE' },
          success: (data) => {
            this.resourceList = hlArrayUtil.replaceData(data, [{key: 'dictName', rKey: 'name'}, {key: 'dictCode', rKey: 'code'}]);
          }
        })
      },
      // 获取收入报表
      getIncome(curPage, limit) {
        var self = this;
        hlhttp.get({
          url: 'report/income',
          data: {
            keyword: this.keyword || "#",
            payment_begin_time: this.payment_begin_time || "#",
            payment_end_time: this.payment_end_time || "#",
            expense_name: this.expense_name.length ? this.expense_name[0] : "#",
            bill_type: this.bill_type.length ? this.bill_type[0] : "#",
            contract_type: this.contract_type.length ? this.contract_type[0] : "#",
            area_id: this.areaId || "#",
            company_id: this.companyId.length ? this.companyId[0] : "#",
            project_id: this.projectId.length ? this.projectId[0] : "#",
            building_id: this.buildingId.length ? this.buildingId[0] : "#",
            floor_id: this.floorId.length ? this.floorId[0] : "#",
            unit_id: this.unitId.length ? this.unitId[0] : "#",
            page_size: this.limit[0],
            page: curPage || "#",
            project_ids: this.project_ids.toString() || "#",
          },
          success: (data) => {

            this.incomeList = data.income
            self.totalCount = data.totalCount;
            $("#page").initPage(data.totalPage, curPage, function (n) {
              if (curPage == n) return false
              self.curPage = n
              self.getIncome(n, limit)
            });

            var TemData = []
            this.costitemList.forEach(item => {
              let a = {
                'expenseName': item.name,
                'amountReceive': "--",
              }
              TemData.push(a)
            })

            this.incomeList.map((item, index) => {
              item.array = []
              TemData = TemData.concat(item.expenses)
            })

            TemData = hlArrayUtil.removeRepeat(TemData, 'expenseName')

            TemData.map(item => {
              item.amountReceive = '--'
            })
            this.costitemList0 = JSON.parse(JSON.stringify(TemData))
            this.incomeList.map((item, index) => {
              item.expenses = item.expenses.concat(TemData)
            })

            this.incomeList.map((item, index) => {
              item.expenses = hlArrayUtil.removeRepeat(item.expenses, 'expenseName')
            })

            this.incomeList.map((item, index) => {
              item.expenses = item.expenses.sort(compareAsc('expenseName'))
            })

          }
        })
      },

      //整理expenses
      setexpenses(data1, data2, index) {
        data1.forEach(item => {
          this.setexpensesList(item, data2, index)
        })
      },

      //整理expensesList
      setexpensesList(item, data2, index) {
        data2.forEach(single => {
          if (single.expenseName == item.expenseName) {
            single.amountReceive = item.amountReceive
          }
        })

      },


      //获取大区下的所有项目
      getConfigprojects() {
        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'REPORT', config_name: 'BI_INCOME_BILL_TYPE' },
          success: (data) => {
            this.billlist = hlArrayUtil.replaceData(data, [{key: 'dictName', rKey: 'name'}, {key: 'dictCode', rKey: 'code'}]);
          }
        })
      },
      // 获取公司下所有项目
      getcompanyProject() {
        hlhttp.get({
          url: 'company/projects',
          data: { company_id: this.companyId[0] },
          success: (data) => {
            this.ProjectList = hlArrayUtil.replaceData(data, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
          }
        })
      },
      //获取公司
      getcompany() {
        hlhttp.get({
          url: 'companys',
          success: (data) => {
            this.companyList = hlArrayUtil.replaceData(data, [{key: 'shortName', rKey: 'name'}, {key: 'compayId', rKey: 'code'}]);
          }
        })
      },
      // 获取楼宇
      getbuildings() {
        hlhttp.get({
          url: 'project/buildings',
          data: { project_id: this.projectId[0] },
          success: (data) => {
            this.buildingList = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
          }
        })
      },
      // 获取楼层
      getfloors() {
        hlhttp.get({
          url: 'building/floors',
          data: { project_id: this.projectId[0], building_id: this.buildingId[0] },
          success: (data) => {
            this.floorList = hlArrayUtil.replaceData(data, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
          }
        })
      },
      //获取单元
      getunits() {
        hlhttp.get({
          url: 'floor/units',
          data: { project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0] },
          success: (data) => {
            this.unitList = hlArrayUtil.replaceData(data, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
          }
        })
      },
      // 选择大区
      selectarea(id) {
        this.areaId = id
        this.companyId = []
          this.projectId = []
            this.buildingId = []
            this.floorId = []
            this.unitId = []

          this.companyList = []
          this.ProjectList = []
          this.buildingList = []
          this.floorList = []
          this.unitList = []
        this.getcompany();
      },
      //选择大区下的公司
      selectcompany(arr) {
        this.companyId = [arr[0].code]
        this.projectId = []
          this.buildingId = []
          this.floorId = []
          this.unitId = []

          this.ProjectList = []
          this.buildingList = []
          this.floorList = []
          this.unitList = []
        this.getcompanyProject();
      },
      // 选择大区公司下的项目
      selectproject(arr) {
        this.projectId = [arr[0].code]
        this.buildingId = []
          this.floorId = []
          this.unitId = []

          this.buildingList = []
          this.floorList = []
          this.unitList = []
        this.getbuildings();
      },
      //选择楼宇
      selectbuilding(arr) {
        this.buildingId = [arr[0].code]
        this.floorId = []
          this.unitId = []

          this.floorList = []
          this.unitList = []
        this.getfloors();
      },
      //选择楼层
      selectfloor(arr) {
        this.floorId = [arr[0].code]
        this.unitId = []

          this.unitList = []
        this.getunits();
      },
      //选择单元
      selectunit(arr) {
        this.unitId = [arr[0].code]
      },
      //选择费项
      selectcostitem(arr) {
        this.expense_name = [arr[0].name]
      },
      // 选择账单类型
      selectbill(arr) {
        this.bill_type = [arr[0].code]
      },
      //选择资源
      selectresource(arr) {
        this.contract_type = [arr[0].code]
      },
      //查询
      search() {
        this.getIncome(1, this.limit[0])
      },
      //重置
      reset() {

        this.keyword = '',
        this.payment_begin_time = '',
        this.payment_end_time = '',
        this.expense_name = []
        this.bill_type = []
        this.contract_type = []

        this.areaId = '',
        this.companyId = []
        this.projectId = []
        this.buildingId = []
        this.floorId =[]
        this.unitId = []
        this.ProjectList = []
        this.buildingList = []
        this.floorList = []
        this.unitList = []

        this.getIncome(1, this.limit[0])
      },
      //分页
      selectPerNum(arr) {
        this.limit = [arr[0].code];
        this.getIncome(1, this.limit[0])
      },
      //导出文件
      incomeExport() {
        hlhttp.get({
          baseUrl: exclUrl,
          url: 'resource/income_export',
          data: {
            keyword: this.keyword || "#",
            payment_begin_time: this.payment_begin_time || "#",
            payment_end_time: this.payment_end_time || "#",
            expense_name: this.expense_name.length ? this.expense_name[0] : "#",
            bill_type: this.bill_type.length ? this.bill_type[0] : "#",
            contract_type: this.contract_type.length ? this.contract_type[0] : "#",
            area_id: this.areaId || "#",
            company_id: this.companyId.length ? this.companyId[0] : "#",
            project_id: this.projectId.length ? this.projectId[0] : "#",
            building_id: this.buildingId.length ? this.buildingId[0] : "#",
            floor_id: this.floorId.length ? this.floorId[0] : "#",
            unit_id: this.unitId.length ? this.unitId[0] : "#",
            project_ids: this.project_ids.toString() || "#",
          },
          success: (data) => {
            var a = document.createElement('a');
            a.href = data.excel_url;
            $('body').append($(a))
            a.click();
            setTimeout(function() {
              $(a).remove()
            }, 100);

          }
        })
      },

    },
    created() {
      hlSelectUi()
      this.getConfigCostItem()
      this.getConfigBillItem()
      this.getConfigResourcesItem()
      this.getProjectName()
      this.getConfigAreas()
    },
    mounted() {
      loginPrem.loginList()
    }
  })

  function compareAsc(propertyName) {
    return function (object1, object2) {
      var value1 = object1[propertyName];
      var value2 = object2[propertyName];
      if (value2 < value1) {
        return 1;
      } else if (value2 > value1) {
        return -1;
      } else {
        return 0;
      }
    }
  }
}())
